/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[TestServletRequest.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package servlet.api;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * javax.servlet.ServletRequest
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "TestServletRequest", urlPatterns = "/servletRequest")
public class TestServletRequest extends GenericServlet {

	private static final long serialVersionUID = -220463981953349512L;

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// 使用/servletRequest?username=Sean&password=12345&hobby=Reading&hobby=Music&hobby=Sports
		// ServletRequest获取页面参数的方法
		// 方式1: 获得指定参数名的参数值
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String hobby = request.getParameter("hobby");
		System.out.println(username + "->" + password + "->" + hobby);

		// 方式2: 获得所有的参数名, 然后根据参数名获得参数值
		Enumeration<String> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			String paramValue = request.getParameter(paramName);
			System.out.println(MessageFormat.format("{0}->{1}", paramName, paramValue));
		}

		// 方式3: 获得一个参数Map, Map的key是参数名, value是参数值数组
		Map<String, String[]> paramMap = request.getParameterMap();
		for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
			String entryKey = entry.getKey();
			String[] entryValue = entry.getValue();
			System.out.println(MessageFormat.format("{0}->{1}", entryKey, Arrays.toString(entryValue)));
		}

		// 方式4: 获得指定参数名的参数值, 用于获得如多选框之类可以有多个值的属性
		String[] hobbies = request.getParameterValues("hobby");
		System.out.println("hobby = " + Arrays.toString(hobbies));

		// 其他方法, 部分方法属于HttpServletRequest
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		System.out.println("Remote address = " + httpRequest.getRemoteAddr());
		System.out.println("Remote host = " + httpRequest.getRemoteHost());
		System.out.println("Remote port = " + httpRequest.getRemotePort());
		System.out.println("Server name = " + httpRequest.getServerName()); // localhost
		System.out.println("Server port = " + httpRequest.getServerPort()); // 8080
		System.out.println("Local address = " + httpRequest.getLocalAddr());
		System.out.println("Local port = " + httpRequest.getLocalPort()); // 8080
		System.out.println("Local name = " + httpRequest.getLocalName()); // sunneverset_pc

		System.out.println("Scheme = " + httpRequest.getScheme()); // http
		System.out.println("Request method = " + httpRequest.getMethod()); // GET
		System.out.println("Protocol = " + httpRequest.getProtocol()); // HTTP/1.1
		System.out.println("Query string = " + httpRequest.getQueryString()); // username=Sean&password=12345&hobby=Reading&hobby=Music&hobby=Travel
		System.out.println("Request URL = " + httpRequest.getRequestURL()); // http://localhost:8080/JavaEE_Web/servletRequest
		System.out.println("Request URI = " + httpRequest.getRequestURI()); // /JavaEE_Web/servletRequest
		System.out.println("Servlet path = " + httpRequest.getServletPath()); // /servletRequest
		System.out.println("Path info = " + httpRequest.getPathInfo()); //
	}
}
